var glNoteCtrl = angular.module('glNoteCtrl', ['glNoteServices']);
glNoteCtrl.controller('SignInController', [
        'WebStorageService',
        '$state',
        'myConfig',
        '$scope',
        'SignInService',
        'ShowStatus',
        function(WebStorageService, $state, myConfig, $scope, SignInService, ShowStatus) {
            var remMe = WebStorageService.getRemMeInfo();
            $scope.checked = remMe.checked;
            $scope.user = {
                email: remMe.email,
                password: ''
            }
            
            $scope.signIn = function(valid) {
                if(valid){
                    SignInService.signIn($scope.user, function(value, responseHeaders) {
                        ShowStatus.show(value,function(resp){
                            if($scope.checked){
                                WebStorageService.saveForRemMe($scope.user.email, $scope.checked);
                            }else{
                                WebStorageService.saveForRemMe('', false);
                            }
                            WebStorageService.saveLoginInfo(resp);
                            $state.go('home');
                        },function(){
                        });
                    }, function(httpResp) {
                        console.log(httpResp);
                    });
                }
            }
        }
    ])
    .controller('SignUpController', ['myConfig',
        'SignUpService',
        '$scope',
        'ShowStatus',
        function(myConfig, SignUpService, $scope, ShowStatus) {
            $scope.user = {
                name: '',
                username: '',
                email: '',
                password: '',
                confirmPwd: ''
            }
            $scope.signUp = function(valid) {
                if(valid){
                    SignUpService.signUp($scope.user, function(value, responseHeaders) {
                        ShowStatus.show(value);
                    });
                }
            }
        }
    ])
    .controller('ResetPwdController', ['ShowStatus', 'myConfig', 'ResetPwdService', '$state', '$scope', function(ShowStatus, myConfig, ResetPwdService, $state, $scope) {
        $scope.user = {
            email: ''
        };
        $scope.resetPwd = function(valid) {
            if(valid){
                ResetPwdService.resetPwd($scope.user, function(value, responseHeaders) {
                    ShowStatus.show(value);
                });
            }
        }
    }])
    .controller('ResendConfirmEmailController', ['ShowStatus', '$scope', '$resource', 'myConfig', function(ShowStatus, $scope, $resource, myConfig) {
        $scope.user = {
            email: ''
        };
        $scope.resendConfirmEmail = function(valid) {
            if(valid){
                var resendEmail = $resource(myConfig.resendConfirmEmail);
                resendEmail.save(null, $scope.user, function(value, responseHeaders){
                    ShowStatus.show(value);
                }, function(httpResponse){
                    console.log(httpResponse);
                });
            }
        }
    }])
    .controller('ChangePwdController', ['$scope','$resource','$stateParams', 'myConfig', 'ShowStatus', function($scope, $resource, $stateParams, myConfig, ShowStatus) {
        $scope.user = {
            password: '',
            auth_token: $stateParams.reset_pwd_token
        };
        $scope.changePwd = function(valid) {
            if(valid){
                var changePwd = $resource(myConfig.changePwdUrl);
                changePwd.save(null,$scope.user,function(value, responseHeaders){
                    ShowStatus.show(value);
                },function(httpResponse){
                    console.log(httpResponse);
                });
            }
        }
    }])
    .controller('HomeController', ['$resource', 'WebStorageService', '$scope', '$state', function($resource, WebStorageService, $scope, $state) {
        var loginInfo = WebStorageService.getLoginInfo();
        if(loginInfo){
            var user = $resource('/rest/user/:userId');
            user.get({userId: loginInfo.id}, function(value, responseHeaders){
                $scope.username = value.username;
                console.log(value);
                console.log(responseHeaders);
            },function(httpResponse){
                console.log(httpResponse);
            });
        }else{
            $state.go('entrance.signIn');
        }
    }]);
